import {
  Controller,
  Get,
  Post,
  Body,
  Patch,
  Param,
  Delete,
  ParseIntPipe,
  Query,
} from '@nestjs/common';
import { ArticleService } from './article.service';
import { CreateArticleDto } from './dto/create-article.dto';
import { UpdateArticleDto } from './dto/update-article.dto';
import { LikeArticleDto } from './dto/like-article.dto';
import { ApiOperation, ApiQuery, ApiTags } from '@nestjs/swagger';
import { CommentArticleDto } from './dto/comment-article.dto';

@ApiTags('文章管理')
@Controller('article')
export class ArticleController {
  constructor(private readonly articleService: ArticleService) {}

  @ApiOperation({ summary: '新增文章' })
  @Post()
  create(@Body() createArticleDto: CreateArticleDto) {
    return this.articleService.create(createArticleDto);
  }

  @ApiOperation({ summary: '分页查询文章列表' })
  @ApiQuery({
    name: 'page',
    type: Number,
    description: '当前页码',
    required: true,
    example: 1,
  })
  @ApiQuery({
    name: 'pageSize',
    type: Number,
    description: '分页大小',
    required: true,
    example: 10,
  })
  @ApiQuery({
    name: 'title',
    type: String,
    description: '标题',
    required: false,
    example: '',
  })
  @ApiQuery({
    name: 'keywords',
    type: String,
    description: '关键词',
    required: false,
    example: '',
  })
  @ApiQuery({
    name: 'classificationId',
    type: String,
    description: '分类',
    required: false,
    example: '',
  })
  @ApiQuery({
    name: 'sortReadNum',
    type: String,
    description: '阅读量排序[DESC:升序]、[ASC降序]',
    required: false,
    example: '',
  })
  @ApiQuery({
    name: 'sortLikeNum',
    type: String,
    description: '点赞量排序[DESC:升序]、[ASC降序]',
    required: false,
    example: '',
  })
  @ApiQuery({
    name: 'sortCommentNum',
    type: String,
    description: '评论数排序[DESC:升序]、[ASC降序]',
    required: false,
    example: '',
  })
  @Get()
  findAll(
    @Query('page', new ParseIntPipe()) page = 1,
    @Query('pageSize', new ParseIntPipe()) pageSize = 10,
    @Query('title') title?: string,
    @Query('keywords') keywords?: string,
    @Query('classificationId') classificationId?: string,
    @Query('sortReadNum') sortReadNum?: string,
    @Query('sortLikeNum') sortLikeNum?: string,
    @Query('sortCommentNum') sortCommentNum?: string,
  ) {
    return this.articleService.findAll({
      page,
      pageSize,
      title,
      keywords,
      classificationId,
      sortReadNum,
      sortLikeNum,
      sortCommentNum,
    });
  }

  @ApiOperation({ summary: '查询文章详情' })
  @Get(':id')
  findOne(@Param('id') id: string) {
    return this.articleService.findOne(id);
  }

  @ApiOperation({ summary: '更新文章' })
  @Patch(':id')
  update(@Param('id') id: string, @Body() updateArticleDto: UpdateArticleDto) {
    return this.articleService.update(id, updateArticleDto);
  }

  @ApiOperation({ summary: '删除文章' })
  @Delete(':id')
  remove(@Param('id') id: string) {
    return this.articleService.remove(id);
  }

  @ApiOperation({ summary: '点赞文章' })
  @Patch('liks/:id')
  liks(@Param('id') id: string, @Body() likeArticleDto: LikeArticleDto) {
    return this.articleService.likeArticle(id, likeArticleDto);
  }

  @ApiOperation({ summary: '阅读文章量+1' })
  @Patch('readCount/:articleId')
  readCount(@Param('articleId') articleId: string) {
    return this.articleService.incrementReadCount(articleId);
  }

  @ApiOperation({ summary: '评论文章' })
  @Post('/comment/:id')
  comment(@Param('id') id: string, @Body() data: CommentArticleDto) {
    return this.articleService.commentArticle(id, data);
  }

  @ApiOperation({ summary: '删除评论' })
  @Delete('/comment/:id')
  deleteComment(@Param('id') id: string, @Body('userId') userId: string) {
    return this.articleService.deleteComment(id, userId);
  }

  @ApiOperation({ summary: '获取文章评论列表' })
  @Get('/comment/list/:articleId')
  getCommentList(@Param('articleId') articleId: string) {
    return this.articleService.getCommentsByArticle(articleId);
  }
}
